<template>
  <div>
    <SearchContainer @search="getData" @reset="resetForm()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="bqbh" class="mx-3">
          <SelectArea v-model:value="formState.bqbh" use-cache set-cache allow-clear />
        </a-form-item>
        <a-form-item name="zjm" label="" class="w-1/5">
          <a-input v-model:value="formState.zjm" placeholder="药品编号/药品名称/助记码" allow-clear />
        </a-form-item>
        <a-form-item name="dateRange" label="发药日期">
          <a-range-picker v-model:value="formState.dateRange" show-time format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss" />
        </a-form-item>
        <a-form-item name="sfph" class="ml-5">
          <a-radio-group v-model:value="formState.sfph">
            <a-radio :value="0">不显示批号</a-radio>
            <a-radio :value="1">显示批号</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item name="sfpj" class="ml-5">
          <a-radio-group v-model:value="formState.sfpj">
            <a-radio value="">全部</a-radio>
            <a-radio :value="0">针剂</a-radio>
            <a-radio :value="1">片剂</a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <div class="flex justify-end mb-3">
        <a-button class="mr-2" type="primary" ghost @click="onPrint"> <BaseIcon name="PrinterOutlined" />汇总发药单 </a-button>
        <BaseExport type="门诊" name="西成药发药汇总" :code="formState.sfph ? '000351' : '000352'" :search-params="formState">导出</BaseExport>
      </div>
      <BaseTable :loading="loading" :column-code="formState.sfph ? '000351' : '000352'" fill :data-source="tableData" bordered :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'mc'">
            <MedicineInfo :value="{ mc: record?.mc, gg: record?.gg, cj: record?.cj }" />
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onHandleEdit(record)"> 明细 </a-button>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" :form-data="formState" />
    <!-- <div class="absolute bottom-0 z-50 p-4 shadow-2xl bg-white"> -->
    <div class="absolute -top-[9999px]">
      <a-button ref="printRef" v-print="{ id: 'printDom' }" type="primary">打印</a-button>
      <PrintView id="printDom" :print-data="printData" :print-type="printType" />
    </div>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import Edit from './Edit.vue'
import PrintView from './PrintView'

const printRef = ref(null)
const printData = ref([])
const printType = ref(2)
const editOpen = ref(false)
const formRef = ref(null)
const rowData = ref({})
const formState = ref({
  dateRange: [dayjs(new Date().setDate(new Date().getDate() - 10)).format('YYYY-MM-DD HH:mm:ss'), dayjs().format('YYYY-MM-DD HH:mm:ss')],
  operationmode: 4,
  sfph: 0,
  sfpj: ''
})
const { loading, tableData, pagination, getData } = useTable({
  url: '/inp/zyyf/getDistributeDrugHzList',
  params: formState.value,
  immediate: true,
  formatParams: (params) => {
    return {
      stdate: params['dateRange']?.[0],
      eddate: params['dateRange']?.[1],
      sfph: formState.value.sfph,
      sfpj: formState.value.sfpj,
      zjm: formState.value.zjm,
      bqbh: formState.value.bqbh,
      operationmode: 2
    }
  }
})

const onHandleEdit = (row) => {
  editOpen.value = true
  rowData.value = row
}

// 打印
const onPrint = async () => {
  const { code, msg, data } = await post('/inp/Print/inpSummaryPrescriptionsIssuedPrint', {
    stdate: formState.value['dateRange']?.[0],
    eddate: formState.value['dateRange']?.[1],
    sfph: formState.value.sfph,
    zjm: formState.value.zjm,
    sfpj: formState.value.sfpj,
    bqbh: formState.value.bqbh,
    operationmode: 2
  })
  if (code !== 0) return message.error(msg)
  if (getSystemConfig('NAVYHIS_MODE') === '1') {
    printType.value = formState.value.sfpj === '' ? 2 : formState.value.sfpj
    printData.value = JSON.parse(data)
    if (!printData.value.printData.detail?.length) return message.error('没有需要打印的数据')
    setTimeout(() => {
      printRef.value.$el.click()
    }, 1000)
  } else {
    window.openExe?.rpt(data) //调用winform打印
  }
}
const resetForm = () => {
  formState.value.dateRange = [dayjs(new Date().setDate(new Date().getDate() - 10)).format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
  formState.value.operationmode = 4
  formState.value.sfph = 0
  formState.value.sfpj = ''
  formState.value.zjm = ''
  getData()
}

watch(
  () => formState.value.sfph,
  () => {
    getData()
  }
)
</script>
